/*****************************************************************************
	SNEGA UTILITIES 0.7a
	Copyright (C) 2009, Xiaohan Tian (snega0223)
	
	http://code.google.com/p/snega-utilities/
	
	This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License(LGPL) as
	published by the Free Software Foundation, either version 3 of the 
	License, or(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.
	
	You should have received a copy of the GNU Lesser General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/

package org.sng.util.web.elmt;

import javax.servlet.http.*;

import org.sng.util.var.*;

public class NodeTextArea extends Node {
	
	public NodeTextArea()
	{
		super();
	}
	
	public NodeTextArea(int n)
	{
		super(n);
	}
	
	public NodeTextArea(String id)
	{
		super();
		
		this.m_properties.put("id", new SString(id));
	}
	
	public NodeTextArea(int n, String id)
	{
		super(n);
		
		this.m_properties.put("id", new SString(id));
	}
	
	public NodeTextArea Create()
	{
		return new NodeTextArea();
	}
	
	public NodeTextArea Create(int n)
	{
		return new NodeTextArea(n);
	}
	
	public NodeTextArea Create(String id, RuntimeEnvironment re)
	{
		HttpServletRequest request = re.GetRequest();
		
		NodeTextArea o = new NodeTextArea();
		
		o.PushProperty("id", new SString(id));
		
		try {
			o.PushProperty("value", new SString(SString.ConvertPostSafeToNormal(request.getParameter(id + "_value"))));		
		} catch (Exception e) {
			o.PushProperty("value", new SString(""));
			e.printStackTrace();
		}
		//SLog.println(request.getParameter(id + "_value") + "|" + o.GetProperty("value").GetString(re));
		
		String postback = request.getParameter(id + "_postback");
		if (postback != null)
			o.PushProperty("postback", postback);
		
		return o;
	}
	
	public String GetType()
	{
		return "textbox";
	}
	
	public StringBuffer Do(RuntimeEnvironment re) throws Exception
	{
		StringBuffer result = new StringBuffer();
		
		SVoid prop = null;
		
		if ((prop = this.m_properties.get("id")) == null)
		{
			throw new Exception("Critical property \"id\" is missing.");
		}
		
		String sid = prop.GetString(re);
		
		String value;
		if ((prop = this.m_properties.get("value")) == null) {
			value = "";
		}
		else {
			value = prop.GetString(re);
		}
		
		StringBuffer properties = new StringBuffer();
		if ((prop = this.m_properties.get("class")) != null) {
			properties.append(" class=\"");
			properties.append(prop.GetString(re));
			properties.append("\"");
		}
		if ((prop = this.m_properties.get("style")) != null) {
			properties.append(" style=\"");
			properties.append(prop.GetString(re));
			properties.append("\"");
		}
		
		result.append("<textarea id=\"" + sid + "\" type=\"text\"" + properties + ">" + value + "</textarea>");
		
		return result;
	}
	
	public void AppendPostList(RuntimeEnvironment re) {
		String sid = this.m_properties.get("id").GetString(re);
		
		re.AppendPostList("if($(\"" + sid + "\")!=null){");
		re.AppendPostList("para.add(\"cid\", \"" + sid + "\");");
		re.AppendPostList("para.add(\"type\", \"TextArea\");");
		re.AppendPostList("para.add(\"" + sid + "_value\", $ConvertToPostSafe($(\"" + sid + "\").value));");
		
		SVoid postback = this.GetProperty("postback");
		if (postback != null)
			re.AppendPostList(sid + "_postback", "\"" + SString.ToStringLiterealSafe(postback.GetString(re)) + "\"");
		
		re.AppendPostList("};");
	}
	
	public void SetValue(RuntimeEnvironment re, String s)
	{
		this.m_properties.put("value", new SString(s));
		
		re.AppendJavascript("$(\"" + this.m_properties.get("id").GetString(re) + "\").value=\"" + SString.ToStringLiterealSafe(this.m_properties.get("value").GetString(re)) + "\";");
	}
	
	public String GetValue(RuntimeEnvironment re)
	{
		return this.GetProperty("value").GetString(re);
	}

}
